package com.sreManager.controller.web;

import java.net.UnknownHostException;
import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sreManager.Constant;
import com.sreManager.domain.LoginUsers;
import com.sreManager.mapper.UsersMapper;
import com.sreManager.web.util.CommonUtil;
import com.sreManager.web.util.LoginManager;

@Controller
@RequestMapping(value={"/user/"})
public class UserController extends CommonController{
	@Autowired UsersMapper usersMapper;

	String TILESForder = "user";
	@ModelAttribute("menuType")
	public String type(){
		return TILESForder;
	}
	/**
	 * 리스트
	 * @return
	 * @throws UnknownHostException 
	 */
	@RequestMapping(value={"list"})
	public String list(Model model, @RequestParam HashMap<String, Object> paramMap, @RequestParam(required=false) String logMsg) throws UnknownHostException{
		// 로그인 유저 정보
		LoginUsers loginUser = LoginManager.getInstance().getSession(request);

        if(loginUser.getRole().equals(Constant.USER)){
			return "redirect:/unuse";
		}
		Integer pageDataTotal = usersMapper.selectUserPagingCount(paramMap);
		model.addAttribute("pageDataTotal", pageDataTotal);
    	if(pageDataTotal!=null && pageDataTotal > 0){
    		paramMap = CommonUtil.pagingNumCheck(paramMap, pageDataTotal);
    		paramMap.put("USER_ROLE_GROUP", Constant.USER_ROLE);
    		paramMap.put("USER_STATE_GROUP", Constant.USER_STATE);
    		model.addAttribute("pagingList", usersMapper.selectUserPagingList(paramMap));
    	}
		model.addAttribute("pageParam", paramMap);

		// 로그 
		if(logMsg != null){
			logsService.insertLog(request, Constant.LOG_TYPE_LIST, Constant.MENU_USER, logMsg);
		}else{
			logsService.insertLog(request, Constant.LOG_TYPE_SEARCH, Constant.MENU_USER);			
		}
		
		return TILESForder+"/list"+Constant.TILES;
	}
}
